package org.kineticsproject.visualizations;
import java.awt.Color;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;


public class KMLWriter {

	protected 	Trace 			    trace;
	protected 	FileOutputStream 	fos;
	protected 	DataOutputStream	dos;
	protected 	File				f;
	//protected 	ErrorHandler        eh;
	protected 	Color               color;
	protected 	boolean             bFirstCrumb;
	protected   String				filename;


	  

	public void init()
	{
		  this.trace 		= trace;
		  f        			= new File(filename);
		//  eh       			= new ErrorHandler();
		  color    			= null;
		  bFirstCrumb		= true;
		//  FileErrorInfo fei = new FileErrorInfo();	  
		  try 
		  {
			  fos = new FileOutputStream(f);
			  dos = new DataOutputStream(fos);
			//  fei.setValid(true);
			 // fei.setFilename(soutputFilename);
		  }
		  catch (IOException ex)
		  {
			 // eh.process(ErrorHandler.FILENOTCREATED_EXCEPTION,fei);
		  }
	}
	  
		public void setFilename(String filename)
		{
			this.filename = filename;
		}
		
		public void setTrace(Trace trace)
		{
			this.trace = trace;
		}
	  
	  protected String colorBlender(String HTMLcolor) // translate HTML hex color to KML hex color
	  {
		  String result;
		  
		  result = HTMLcolor.substring(0,2); 
		  result = result.concat(HTMLcolor.substring(6,8));
		  result = result.concat(HTMLcolor.substring(4,6));
		  result = result.concat(HTMLcolor.substring(2,4));



		  return(result);
	  }
	  
	  protected void close()
	  {
		  //FileErrorInfo fei = new FileErrorInfo();	
		  try
		  {
			  fos.close();
		  }
		  catch (IOException ex)
		  {
			//  eh.process(ErrorHandler.FILENOTCLOSED_EXCEPTION,fei);
		  }
	  }
	  
	  protected void writeKMLFooter()
	  {
		 // FileErrorInfo fei = new FileErrorInfo();
		  try
		  {
			  dos.writeBytes("    </coordinates>\n"+
					         "  </LineString>\n"+
					         "</Placemark>\n"+
					         "</Document>\n"+
					         "</kml>\n");
		  }
		  catch(IOException ex)
		  {
			 // eh.process(ErrorHandler.FILEWRITE_EXCEPTION,fei);
		  }
	  }
	  protected void writeKMLHeader(String sLat, String sLon, String sRange, String sTilt, String sHeading)
	  {
		 // FileErrorInfo fei = new FileErrorInfo();
		  try
		  {
			
		  dos.writeBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
				  	     "<kml xmlns=\"http://earth.google.com/kml/2.0\">\n"+
				  	     "<Document>\n"+
				         "<Placemark>\n"+
				         "  <description>KMLGenerator</description>\n"+
				         "  <name>Absolute Extruded</name>\n"+
		               	 "  <LookAt>\n"+
		               	 "    <longitude>"+sLon+"</longitude>\n"+
		               	 "    <latitude>"+sLat+"</latitude>\n"+
		               	 "    <range>"+sRange+"</range>\n"+
		               	 "    <tilt>"+sTilt+"</tilt>\n"+
		               	 "    <heading>"+sHeading+"</heading>\n"+
		               	 "  </LookAt>\n"+
		               	 "  <visibility>1</visibility>\n"+
		               	 "  <open>0</open>\n");
		         /*      	 "  <Style>\n"+
		               	 "    <LineStyle>\n"+
		               	 "      <color>ff00ffff</color>\n"+
		               	 "    </LineStyle>\n"+
		               	 "    <PolyStyle>\n"+
		               	 "      <color>7f00ff00</color>\n"+
		               	 "    </PolyStyle>\n"+
		               	 "  </Style>\n"+*/
		          //     	 "  <LineString>\n"+
		          //    	 "    <extrude>0</extrude>\n"+
		           //    	 "    <tessellate>0</tessellate>\n"+
		            //   	 "    <altitudeMode>absolute</altitudeMode>\n"+
		            //     "    <coordinates>\n");
	           }
		  catch (IOException ex)
		  {
			//  eh.process(ErrorHandler.FILEWRITE_EXCEPTION,fei);
		  }
	  }
	  
	  public boolean process()
	  {
		  boolean result = true;
		  Crumb previousCrumb = trace.next();
		  Crumb crumb = trace.next();
		  writeKMLHeader(crumb.getSLat(),crumb.getSLon(),"1000","44","-100");
		  
		   while(crumb != null)
		   {  
			   writeCrumb(crumb,previousCrumb);
			   previousCrumb = crumb;
			   crumb = trace.next();
		   }
		   writeKMLFooter();
		   close();
		  return(result);
	  }
	  
	  private void writeCrumb(Crumb c,Crumb previousCrumb)
	  {
		//FileErrorInfo fei = new FileErrorInfo();
	    if (bFirstCrumb)  // change trace color
	    {
	    	
	    try
	    {
	    	bFirstCrumb = false;
	    	dos.writeBytes("<Style>\n"+
	    			       "<LineStyle>\n"+
	    			       "  <color>"+colorBlender(Integer.toHexString(c.getColor().getRGB()))+"</color>\n"+
	                       "<width>"+c.getWidth()+"</width>"+
	    			       "</LineStyle>\n"+
	    			       "</Style>\n"+
	    			       "<LineString>\n"+
	    			       "<coordinates>\n");
	    }
	    catch(IOException ex)
	    {
	    	//eh.process(ErrorHandler.FILEWRITE_EXCEPTION,fei);
	    }
	    }
	    
	    else if (c.getColor() != color)  // change trace color
	    {
	    try
	    {
	    	dos.writeBytes("</coordinates>\n"+
	    			       "</LineString>\n"+
	    			       "</Placemark>\n"+
	    			       "<Placemark><name>"+c.getSLat()+"</name>\n"+
	    			       "<Style>\n"+
	    			       "<LineStyle>\n"+
	    			       "  <color>"+colorBlender(Integer.toHexString((color=c.getColor()).getRGB()))+"</color>\n"+
	    		           "<width>"+c.getWidth()+"</width>"+
	    			       "</LineStyle>\n"+ 
	    			  /*     "<LabelStyle><color>ed7f0055</color><colorMode>normal</colorMode><scale>1.4</scale></LabelStyle>"+
	    			  
	    			       */"</Style>\n"+
	    			       "<LineString>\n"+   			       
	    			       "<coordinates>\n");
	    	/* Write the last crumb again so there isn't a gap between lines */
	    	if (!c.comparePosition(previousCrumb))
	    		dos.writeBytes("     "+previousCrumb.getSLon()+","+previousCrumb.getSLat()+","+previousCrumb.getSAlt()+"\n");
	    }
	    catch(IOException ex)
	    {
	    	//eh.process(ErrorHandler.FILEWRITE_EXCEPTION,fei);
	    }
	    }
		  try
		  {
			  dos.writeBytes("     "+c.getSLon()+","+c.getSLat()+","+c.getSAlt()+"\n");
		  }
		  catch (IOException ex)
		  {
			 // eh.process(ErrorHandler.FILEWRITE_EXCEPTION,fei);
		  }

	  }
	
}
